import QtQuick
import QtQuick.Layouts
import QtQuick.Controls
import QtQuick.Effects

Rectangle {
    id: root
    Theme { id: theme }

    // === 外部属性与信号 ===
    property var model: []
    property string defaultText: ""
    property bool showInputField: false
    property bool passwordField: false
    property bool passwordVisible: false

    // === 样式控制 ===
    property bool backgroundVisible: false
    property real radius: 30
    property int fontSize: 24
    property color buttonColor: theme.secondaryColor
    property color hoverColor: Qt.darker(buttonColor, 1.2)
    property color textColor: theme.textColor
    property color checkmarkColor: theme.focusColor
    property color uncheckedBoxColor: "#F0F7FF"
    property color boxBorderColor: "#B3B3B3"
    property color inputFieldColor: "#F0F7FF"
    property color inputBorderColor: Qt.gray
    property real pressedScale: 0.96
    property bool shadowEnabled: true
    property color shadowColor: theme.shadowColor

    // === 布局尺寸 ===
    property int horizontalPadding: 10
    property int boxSize: 36
    property int buttonHeight: 72
    property int inputFieldHeight: 48
    property int inputFieldWidth: 135
    property int spacingBoxToInput: 15
    property int spacingInputToText: 22
    property int spacingBetweenBoxAndText: 22

    signal toggled(int index, bool checked)

    // === 外部方法：获取已选项 ===
    function getRowObject() {
        var row = {
            ll:root.model[0].defaultInput,
            llStatus:root.model[0].checked,
            l:root.model[1].defaultInput,
            lStatus:root.model[1].checked,
            h:root.model[2].defaultInput,
            hStatus:root.model[2].checked,
            hh:root.model[3].defaultInput,
            hhStatus:root.model[3].checked,
        };
        console.log("model[0].defaultInput", model[0].defaultInput)
        return row;
    }


    // === 文本宽度测量 ===
    Text {
        id: measureText
        visible: false
        font.pixelSize: root.fontSize
    }

    property real maxTextWidth: 0

    // === 尺寸计算 ===
    implicitWidth: model.length > 0
        ? (model.length * (
            horizontalPadding * 2 +
            boxSize +
            (showInputField ? spacingBoxToInput + inputFieldWidth + spacingInputToText : spacingBetweenBoxAndText) +
            maxTextWidth
        )) + (model.length - 1) * spacingBetweenBoxAndText
        : (horizontalPadding * 2 +
           boxSize +
           (showInputField ? spacingBoxToInput + inputFieldWidth + spacingInputToText : spacingBetweenBoxAndText) +
           maxTextWidth)

    implicitHeight: buttonHeight + 20
    width: implicitWidth
    height: implicitHeight
    color: "transparent"

    // === 背景 ===
    Rectangle {
        id: background
        anchors.fill: parent
        clip: true
        radius: root.radius
        color: root.buttonColor
        visible: root.backgroundVisible

        layer.enabled: root.shadowEnabled && root.backgroundVisible
        layer.effect: MultiEffect {
            shadowEnabled: root.shadowEnabled
            shadowColor: root.shadowColor
            shadowBlur: theme.shadowBlur
            shadowHorizontalOffset: theme.shadowXOffset
            shadowVerticalOffset: theme.shadowYOffset
        }
    }

    // === 主布局 ===
    RowLayout {
        id: buttonsRow
        anchors.top: parent.top
        anchors.bottom: parent.bottom
        anchors.margins: 10
        spacing: spacingBetweenBoxAndText
        Layout.alignment: Qt.AlignVCenter
        width: implicitWidth
        height: implicitHeight
    }

    // === 按钮列表 ===
    Repeater {
        model: root.model
        parent: buttonsRow

        Rectangle {
            id: btn
            height: buttonHeight
            radius: root.radius * 0.5

            property bool hovered: false

            color: root.backgroundVisible
                ? (hovered ? root.hoverColor : root.buttonColor)
                : "transparent"

            Behavior on color { ColorAnimation { duration: 150 } }

            transform: Scale {
                id: scale
                origin.x: btn.width / 2
                origin.y: btn.height / 2
            }

            ParallelAnimation {
                id: restoreAnimation
                SpringAnimation { target: scale; property: "xScale"; to: 1.0; spring: 2.5; damping: 0.25 }
                SpringAnimation { target: scale; property: "yScale"; to: 1.0; spring: 2.5; damping: 0.25 }
            }

            // === 固定宽度的布局容器 ===
            RowLayout {
                anchors.fill: parent
                anchors.leftMargin: horizontalPadding
                anchors.rightMargin: horizontalPadding
                Layout.alignment: Qt.AlignVCenter
                spacing: spacingBoxToInput

                // 1. 复选框（固定宽度）
                Rectangle {
                    id: box
                    width: boxSize
                    height: boxSize
                    radius: boxSize * 0.15
                    border.color: root.boxBorderColor
                    border.width: 2
                    color: modelData.checked === 1 ? root.checkmarkColor : root.uncheckedBoxColor

                    Behavior on color { ColorAnimation { duration: 150 } }

                    Text {
                        anchors.centerIn: parent
                        visible: modelData.checked === 1
                        text: "\uf00c"
                        font.pixelSize: 16
                        font.family: iconFont.name
                        color: "#F0F7FF"
                    }

                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        cursorShape: Qt.PointingHandCursor
                        acceptedButtons: Qt.LeftButton

                        onEntered: btn.hovered = true
                        onExited: btn.hovered = false

                        onReleased: {
                            restoreAnimation.restart()
                            btn.opacity = 1.0
                        }
                        onClicked: {
                            modelData.checked = (modelData.checked === 1 ? 0 : 1);
                            root.model[index].checked = modelData.checked;
                            root.toggled(index, modelData.checked === 1)
                        }
                    }
                }

                // 2. 输入框（固定宽度）
                TextField {
                    id: inputField
                    visible: root.showInputField
                    width: root.inputFieldWidth
                    height: parent.height * 0.7
                    font.pixelSize: root.fontSize
                    color: "#2E394C"
                    placeholderTextColor: "#2E394C"
                    text: modelData.defaultInput || ""
                    
                    // 限制只能输入数字和小数点
                    validator: RegularExpressionValidator {
                        regularExpression: /^[0-9]*\.?[0-9]*$/
                    }
                    
                    // 输入方法提示
                    inputMethodHints: Qt.ImhDigitsOnly
                    
                    background: Rectangle {
                        border.width: 0
                        border.color: "#616366"
                        radius: 9
                        color: "transparent"
                        implicitWidth: root.inputFieldWidth
                        implicitHeight: root.inputFieldHeight

                        Image {
                            source: "qrc:/assets/bjinput.png"
                            anchors.fill: parent
                            fillMode: Image.Stretch
                        }

                    }

                    onTextChanged: {
                        if (root.model && index >= 0 && index < root.model.length) {
                            root.model[index].defaultInput = text;
                        }
                    }
                }

                // 3. 文本标签
                Item {
                    id: labelContainer
                    width: 120
                    height: parent.height
                    Layout.alignment: Qt.AlignVCenter

                    Text {
                        id: label
                        text: modelData.text || ""
                        color: root.textColor
                        font.pixelSize: root.fontSize
                        elide: Text.ElideRight
                        anchors.left: parent.left
                        anchors.right: parent.right
                        anchors.verticalCenter: parent.verticalCenter
                        horizontalAlignment: Text.AlignLeft
                        wrapMode: Text.NoWrap
                    }
                }

                Rectangle {
                    visible: !root.showInputField
                    width: root.inputFieldWidth + spacingInputToText
                    height: 1
                    color: "transparent"
                }
            }


            implicitWidth: horizontalPadding * 2 +
                boxSize +
                (root.showInputField ? spacingBoxToInput + root.inputFieldWidth + spacingInputToText : spacingBetweenBoxAndText) +
                120
        }
    }
}
